package com.lxl.demo;

import java.util.Arrays;

/*
*   0
* 输入：nums1 = [1,3], nums2 = [2]
输出：2.00000
解释：合并数组 = [1,2,3] ，中位数 2*/
public class Demo9 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] nums = new int[nums1.length+nums2.length];
        for (int i = 0; i < nums1.length; i++) {
            nums[i] = nums1[i];
        }
        for (int i = 0; i < nums2.length ; i++) {
            nums[i+nums1.length] = nums2[i];
        }
        Arrays.sort(nums);
        if (nums.length % 2 == 0) {
            int c = nums.length / 2;
            return (nums[c] +nums[c-1]) /2.0;
        } else {
            int c = (nums.length-1) / 2;
            return nums[c]/1.0;
        }
    }

    public static void main(String[] args) {
        Demo9 demo9 = new Demo9();
        double medianSortedArrays = demo9.findMedianSortedArrays(new int[]{1, 2}, new int[]{3});
        System.out.println("medianSortedArrays = " + medianSortedArrays);
    }
}
